import { TOKEN_KEY } from "@/constants";
import type { Profile } from "@/types/user";
import Cookies from "js-cookie";
import { defineStore } from "pinia";
import { computed, ref } from "vue";

// 全局状态管理用户模块11
export const useUserStore = defineStore("hm-user", () => {
  // token
  const token = ref(Cookies.get(TOKEN_KEY) || "");

  // 保存 token3122
  const setToken = (val: string) => {
    // 存储 token 到 store 中，内存访问速度更快
    token.value = val;
    // 通过 Cookies 存储 token（后台管理系统较为常见）
    Cookies.set(TOKEN_KEY, val);
  };

  // 清除 token
  const removeToken = () => {
    token.value = "";
    Cookies.remove(TOKEN_KEY);
  };

  // 个人信息
  const profile = ref<Profile>({} as Profile);
  const setProfile = (p: Profile) => {
    profile.value = p;
  };

  // 记得在最后 return 出去
  return {
    token,
    setToken,
    removeToken,
    profile,
    setProfile,
  };
});
